var _ = require('lodash');

module.exports = function (global) {


  return {
    global: global,

    build: function () {
      var attributes = global.attributes;
      _.each(attributes, function (att, key) {
        var page = att.page;
        if (page.validate) {

        }
      })
    },

    /**
     <div class="form-group" ng-class="{'has-error':<%= obj %>ModalForm.<%=key%>.$invalid && <%= obj %>ModalForm.<%=key%>.$dirty}">
     <label for="<%= obj + fn_upper_first(key) %>"><%=val.page.desc %></label>
     <input type="text" name="<%=key%>" id="<%= obj + fn_upper_first(key) %>" placeholder="<%=val.page.tip %>" ng-model="<%= obj %>.<%=key%>" class="form-control" <% _.each(val.page.validate, function(val, key){  %><%= key + '=\"' + val + '\" '%><%})%> />
     <span class="help-block has-error" ng-if="<%= obj %>ModalForm.<%=key%>.$dirty">
     <span ng-show="<%= obj %>ModalForm.<%=key%>.$error.required"><%=val.page.desc %>是必填项.</span>
     <span ng-show="<%= obj %>ModalForm.<%=key%>.$error.maxlength"><%=val.page.desc %>长度不能大于20.</span>
     </span>
     </div>
     * @param validate
     */
    //buildGroup:
    'hasError': function (attr, key) {
      //无需校验
      if (!attr.page.validate) {
        return '';
      }

      var modalFormKey = global.obj + 'ModalForm.' + key;

//ng-class="{'has-error':<%= obj %>ModalForm.<%=key%>.$invalid && <%= obj %>ModalForm.<%=key%>.$dirty}"
      var res = 'ng-class="{\'has-error\':' + modalFormKey + '.$invalid && ' + modalFormKey + '.$dirty}"';
      return res;
    },

    'label': function () {
      //TODO
    },
    'required': function(attr, key) {
      //无需校验
      if (!attr.page || !attr.page.validate) {
        return '';
      }
      if(attr.page.validate.required) {
        return "(必填)";
      }
    },

    'input': function (attr, key) {
      var groupKey = global.obj + '.' + key;

      //无需校验
      if (!attr.page || !attr.page.validate) {
        var arr = [
          'type="text"',
          'name="' + key + '"',
          'placeholder="' + (attr.page ? (attr.page.tip || '') : '') + '"',
          'ng-model="' + groupKey + '"',
        ];
        return arr.join(' ');
      }

      var arr = [];
      var validate = attr.page.validate;
      var type = validate.type || 'text';
      arr.push('type="' + type + '"');
      arr.push(
        'name="' + key + '"',
        'placeholder="' + (attr.page.tip || '') + '"',
        'ng-model="' + groupKey + '"'
      );
      _.each(validate, function (val, k) {
        if (k == 'type') {
          return;
        }
        arr.push(k + '="' + val + '"');
      });
      return arr.join(' ');
    },

    //error tip
    //<span class="help-block has-error" ng-if="<%= obj %>ModalForm.<%=key%>.$dirty">
    //<span ng-show="<%= obj %>ModalForm.<%=key%>.$error.required"><%=val.page.desc %>是必填项.</span>
    //<span ng-show="<%= obj %>ModalForm.<%=key%>.$error.maxlength"><%=val.page.desc %>长度不能大于20.</span>
    //</span>
    'errorTip': function (attr, key) {
      //无需校验
      if (!attr.page.validate) {
        return '';
      }

      var modalFormKey = global.obj + 'ModalForm.' + key;

      var s = '\t<span class="help-block has-error" ng-if="' + modalFormKey + '.$dirty">';

      var validate = attr.page.validate;
      var spanArr = [s];
      _.each(validate, function (val, k) {
        if (k == 'type') {
          return;
        }
        var rule = k;
        if (_.startsWith(k, 'ng-')) {
          rule = k.substr(3);
        }
        var tip = '';
        switch (rule) {
          case 'required':
            tip = attr.page.desc + '是必填项.';
            break;
          case 'maxlength':
            tip = attr.page.desc + '不能多于' + val + '个字符.';
            break;
          case 'minlength':
            tip = attr.page.desc + '不能少于' + val + '个字符.';
            break;
          case 'min':
            tip = attr.page.desc + '不能小于' + val + '.';
            break;
          case 'max':
            tip = attr.page.desc + '不能大于' + val + '.';
            break;

        }

        var span = '<span ng-show="' + modalFormKey + '.$error.' + rule + '">' + tip + '</span>';
        spanArr.push('\t\t' + span);
      });

      var e = '\t</span>';
      spanArr.push(e);

      return spanArr.join('\r\n');
    }
  };

};
